from flask import request
from config import app, redis_service
from services import user_service
from services import bank_service
from services import home_service


from results import *


# @app.before_request
# def before():
#     url = request.path
#     if url == '/login' or url == '/register':
#         pass
#     else:
#         token = request.headers.get('token')
#         if token is None:
#             return not_login_result
#         username = redis_service.get(token)
#         if username:
#             pass
#         else:
#             return not_login_result


@app.post('/login')
def login():
    get_json = request.json
    return user_service.login_impl(get_json)


@app.post('/register')
def register():
    """_summary_
    注册接口
    传入参数:
    @
    Returns:
        _type_: _description_
    """
    get_json = request.json
    return user_service.register_impl(get_json)

@app.post('/user_info_query')
def user_info_query():
    """
    获取用户信息查询接口
    @输入参数:
    address:
    @输出参数:
    ..
    """
    get_json = request.json
    return user_service.query_user_info(get_json['address'])

@app.post('/user_balance_update')
def user_balance_update():
    get_json = request.json
    return user_service.update_user_balance(get_json['address'], get_json['balance'])

@app.post('/user_transaction_submit')
def user_transaction_submit():
    """_summary_
    “客户”角色提交交易
    Returns:
        _type_: _description_
    """
    get_json = request.json
    return user_service.submit_transaction(get_json['sendAddress'], 
                                           get_json['receiveAddress'], 
                                           get_json['balance'])

@app.post('/query_user_tx')
def query_user_tx():
    """_summary_
    查询用户交易
    Returns:
        _type_: _description_
    """
    get_json = request.json
    return user_service.query_user_transaction(get_json['address'])

@app.post('/query_bank_send_tx')
def bank_send_tx_query():
    """
    查询"银行"对应发送交易的信息
    """
    get_json = request.json
    return bank_service.query_bank_send_tx(get_json['address'])

@app.post('/bank_freeze_and_send')
def bank_freeze_and_send():
    get_json = request.json
    return bank_service.freeze_account_and_send_tx(get_json['address'], get_json['tid'])

@app.post('/query_bank_receive_tx')
def bank_receive_tx_query():
    """
    查询"银行"对应发送交易的信息
    """
    get_json = request.json
    return bank_service.query_bank_receive_tx(get_json['address'])

@app.post('/confirm_tx')
def confirm_tx():
    get_json = request.json
    return bank_service.confirm_tx(get_json['tid'])

@app.post('/reject_tx')
def reject_tx():
    get_json = request.json
    return bank_service.reject_tx(get_json['tid'])

@app.get('/home_page_query')
def home_page_query():
    return home_service.home_page_query()

if __name__ == '__main__':
    app.run()
